package com.jhhc.StormSurgeForecast.Calculate.util;

import com.jhhc.RHDB.Data.Pojo.ST_TIDE_R;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.BDMS_FBC_TFPATH_F;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.BDMS_FBC_TFPATH_R;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.BDMS_ST_ASTROTD_F;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Util.TimeObject;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Util.TimeUtil;
import com.jhhc.StormSurgeForecast.StormSurge.Pojo.FBC.BDMS_FBC_OCEANMODEL_B;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * @author LYQ
 * 生成模型计算所需要的文件工具类
 */
public class CreateFileUtil {

    /**
     * 智能预报模型nuhp1.txt文件
     * @param nhupStart 起报时间
     * @param nhupEnd 结束时间
     * @param localPath 本地路径
     */
    public File createNuhp1(String nhupStart,String nhupEnd,String localPath){
        try {
            TimeUtil timeUtil = new TimeUtil();
            //创建本地文件
            File pathFile = ModelUtil.createFile(localPath,"nhup1.dat");
            FileOutputStream fos = new FileOutputStream(pathFile,true);
            byte[] sBytes;
            StringBuilder value = new StringBuilder();
            TimeObject start = timeUtil.currentTimes1(nhupStart);
            TimeObject end = timeUtil.currentTimes1(nhupEnd);
            value.append(start.getYear()).append(" ").append(start.getMonth()).append(" ").append(start.getDay()).
                    append(" ").append(start.getHour()).append("\r\n").append(end.getYear()).append(" ").
                    append(end.getMonth()).append(" ").append(end.getDay()).append(" ").append(end.getHour());
            sBytes = value.toString().getBytes();
            fos.write(sBytes);
            fos.flush();
            fos.close();
            return pathFile;
        }catch (IOException e) {
            e.printStackTrace();
            System.out.println("文件输出异常");
            return null;
        }
    }
    /**
     * 智能预报模型tfpath文件
     * @param bdmsFbcTfpathRs 实测路径
     * @return 创建是否成功
     */
    public File createTfpathFile(List<BDMS_FBC_TFPATH_R> bdmsFbcTfpathRs, String localPath){
        try {
            TimeUtil timeUtil = new TimeUtil();
            File pathFile = ModelUtil.createFile(localPath,"tfpath.dat");
            FileOutputStream fos = new FileOutputStream(pathFile,true);
            byte[] sBytes;
            StringBuilder value = new StringBuilder();
            for (int i = 0; i < bdmsFbcTfpathRs.size(); i++) {
                TimeObject timeObject = timeUtil.currentTimes1(bdmsFbcTfpathRs.get(i).getYmdhm());
                value.append(timeObject.getYear()).append(" ").append(timeObject.getMonth()).append(" ").
                        append(timeObject.getDay()).append(" ").append(timeObject.getHour()).append(" ").
                        append(bdmsFbcTfpathRs.get(i).getLttd()).append(" ").append(bdmsFbcTfpathRs.get(i).getLgtd()).append(" ").
                        append(bdmsFbcTfpathRs.get(i).getPressure()).append(" ").append(bdmsFbcTfpathRs.get(i).getMaxspeed());
                if (i != bdmsFbcTfpathRs.size() - 1) {
                    value.append("\r\n");
                }
            }
            sBytes = value.toString().getBytes();
            fos.write(sBytes);
            fos.flush();
            fos.close();
            return pathFile;
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("创建tfpath.dat文件异常");
            return null;
        }
    }

    /**
     * 智能预报模型st_tide_r.txt文件
     * @param rList 实测水位数据
     * @param localPath 本地路径
     */
    public File createTide(List<ST_TIDE_R> rList, String localPath){
        try {
            TimeUtil timeUtil = new TimeUtil();
            //创建本地文件
            File pathFile = ModelUtil.createFile(localPath,"st_tide_r.txt");
            FileOutputStream fos = new FileOutputStream(pathFile,true);
            byte[] sBytes;
            StringBuilder value = new StringBuilder();
            for (int i = 0; i < rList.size(); i++) {
                value.append(rList.get(i).getStcd()).append("\t").append(timeUtil.timeFormatting1(rList.get(i).getTm()))
                        .append("\t").append(String.format("%.2f",rList.get(i).getTdz()));
                if (i != rList.size() - 1) {
                    value.append("\r\n");
                }
            }
            sBytes = value.toString().getBytes();
            fos.write(sBytes);
            fos.flush();
            fos.close();
            return pathFile;
        }catch (IOException e) {
            e.printStackTrace();
            System.out.println("文件输出异常");
            return null;
        }
    }


    /**
     * 智能预报模型bdms_st_astrotd_f.txt文件
     * @param fList 天文潮数据
     * @param localPath 本地路径
     */
    public File createAstrotd(List<BDMS_ST_ASTROTD_F> fList, String localPath){
        try {
            TimeUtil timeUtil = new TimeUtil();
            //创建本地文件
            File pathFile = ModelUtil.createFile(localPath,"bdms_st_astrotd_f.txt");
            FileOutputStream fos = new FileOutputStream(pathFile,true);
            byte[] sBytes;
            StringBuilder value = new StringBuilder();
            for (BDMS_ST_ASTROTD_F astrotdF : fList) {
                value.append(astrotdF.getStcd()).append("\t").append(timeUtil.timeFormatting1(astrotdF.getYmdh()))
                        .append("\t").append(String.format("%.2f",astrotdF.getFtdz())).append("\t").
                append(astrotdF.getHltdmk()).append("\t").append(astrotdF.getUnitname()).append("\r\n");
            }
            sBytes = value.toString().getBytes();
            fos.write(sBytes);
            fos.flush();
            fos.close();
            return pathFile;
        }catch (IOException e) {
            e.printStackTrace();
            System.out.println("文件输出异常");
            return null;
        }
    }
}
